package com.simons.common.text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternOper {

	public static List<String> pattern(String regEx, String src){
		Pattern pattern = Pattern.compile(regEx);
		return pattern(pattern, src);
	}
	
	public static List<String> pattern(Pattern pattern, String src){
		Matcher matcher = pattern.matcher(src);
		return patternWithoutMatch(matcher);
	}
	
	public static boolean matches(Matcher matcher){
		return matcher.matches();
	}
	
	/**
	 * @param matcher 执行过匹配的matcher
	 * @return
	 */
	public static List<String> pattern(Matcher matcher){
		List<String> result = new ArrayList<String>();
		for(int i = 0; i <= matcher.groupCount(); i ++){
			String matchStr = matcher.group(i);
			result.add(matchStr);
		}
		
		return result;
	}
	
	/**
	 * 未执行过匹配获取匹配结果
	 * @param matcher
	 * @return
	 */
	public static List<String> patternWithoutMatch(Matcher matcher){
		if(!matcher.matches()){
			return Collections.emptyList();
		}
		
		return pattern(matcher);
	}
	
}
